package com.yxuan.springboot.util;

import java.util.Random;

public class IdUtil {

    // 生成规则：13位时间戳+userid最后一位+type
    public static String createCommentAndReplayCommentId(String userId,String type){
        // 获取 13位时间戳
        String time = String.valueOf(System.currentTimeMillis());
        // 获取userid最后一位
        char lastChar = userId.charAt(userId.length() - 1);
        // 拼接
        String id = time + lastChar + type;
        return id;
    }



    //生成时间戳-随机数用于生成user_id等唯一的字符串
    public static String generateUniqueId() {
        long timestamp = System.currentTimeMillis();
        Random random = new Random();
        int randomNumber = random.nextInt(100000);
        String userId = timestamp + "-" + randomNumber;
        return userId;
    }


    public static int getIntUserId() {
        // 获取当前时间戳（秒）
        long timestamp = System.currentTimeMillis();

        // 截取时间戳的后5位
        String lastFive = String.valueOf(timestamp).substring(String.valueOf(timestamp).length() - 5);

        // 生成5位随机数字
        Random random = new Random();
        String randomFive = String.format("%05d", random.nextInt(100000));

        // 拼接时间戳的后5位和5位随机数字
        String combined = lastFive + randomFive;

        // 将拼接后的字符串转换为int类型
        int combinedInt = Integer.parseInt(combined);
        return combinedInt;
    }
}
